<!--查看供应商采购列表-->
<template>
  <div>
    <cus-card :showTitle="false" style="padding: 20px 20px">
      <cusTable
        border
        :loading="form.loading"
        :columns="columns"
        :data="data"
        :size="vuex_config.tableSize"
        :bottomClass="['cus-page','cus_operBox']"
      >
        <template slot-scope="{ row }" slot="createTime">
          {{ new Date(row.createTime).Format("yyyy-MM-dd hh:mm:ss") }}
        </template>
        <template slot-scope="{ row }" slot="operation">
          <div class="flex">
            <cus-button
              type="text"
              size="small"
              :style="{ color: '#1E6FFF' }"
              style="padding: 0 5px"
              @onClick="onShow(row)"
            >
              查看
            </cus-button>
          </div>
        </template>
      </cusTable>
      <div class="cus_page">
        <cus-page
          :data="pages"
          class="mt_20"
          @pageChange="postApiPurchasingPurchasinglistApi()"
        ></cus-page>
      </div>
    </cus-card>
    <div class="cus_operBox" style="width: calc(100% - 250px)">
      <Button type="error" class="mr_20" @click="onPrevious()">返回</Button>
      <Button
        type="primary"
        class="mr_20"
        @click="getApiPurchasingPoorderelistGeneratePoOrderApi()"
        >生成采购单</Button
      >
      <Button type="primary" class="mr_20" @click="postApiPurchasingSupplierPurchasingPurchasingSummaryApi()"
        >生成采购需求汇总表</Button
      >
    </div>
    <showSupplierPurchase ref="showSupplierPurchase"></showSupplierPurchase>
  </div>
</template>

<script>
import {
  postApiPurchasingPurchasinglist,
  postApiPurchasingSupplierPurchasingPurchasingSummary,
  getApiPurchasingPoorderelistGeneratePoOrder,
  postApiFoodBookServiceAuditConfirmPurchasingPlan,
} from "@/api/business/manager";
import showSupplierPurchase from "./components/showSupplierPurchase.vue";
const form = {
  loading: false,
  id: "",
  planId: "", //食谱id
};

const columns = [
  {
    title: "序号",
    type: "index",
    minWidth: 70,
    align: "center",
  },
  {
    title: "供应商名称",
    minWidth: 110,
    key: "supplyName",
    align: "center",
  },
  {
    title: "供应商种类",
    minWidth: 210,
    key: "supplyClass",
    align: "center",
  },
  {
    title: "采购计划名称",
    minWidth: 210,
    key: "purchasingPlanName",
    align: "center",
  },
  {
    title: "上周日",
    minWidth: 90,
    key: "lastSunday",
    align: "center",
  },
  {
    title: "周一",
    minWidth: 90,
    key: "monday",
    align: "center",
  },
  {
    title: "周二",
    minWidth: 90,
    key: "tuesday",
    align: "center",
  },
  {
    title: "周三",
    minWidth: 90,
    key: "wednesday",
    align: "center",
  },
  {
    title: "周四",
    minWidth: 90,
    key: "thursday",
    align: "center",
  },
  {
    title: "周五",
    minWidth: 90,
    key: "friday",
    align: "center",
  },
  {
    title: "周六",
    minWidth: 90,
    key: "saturday",
    align: "center",
  },
  {
    title: "周日",
    minWidth: 90,
    key: "sunday",
    align: "center",
  },
  {
    title: "采购来源",
    key: "type",
    minWidth:110,
    align: "center",
    render: (h, params) => {
      return h("div", [
        h("span", {}, params.row.type == 0 ? "手工录入" : "食谱采购"),
      ]);
    },
  },
  {
    title: "操作",
    minWidth: 135,
    fixed: "right",
    slot: "operation",
    align: "center",
  },
];
export default {
  name: "supplierPurchase",
  components: { showSupplierPurchase },

  data() {
    return {
      form: form,
      columns: columns,
      data: [],
      pages: {
        page: 1,
        pageSize: 20,
        pageTotal: 0,
      },
      tableHeight: 0,
    };
  },
  mounted() {
    setTimeout(() => {
      this.tableHeight = window.innerHeight - 250; // 表格自适应高度
    }, 100);
    window.addEventListener("resize", this.tableHeightChange, true);
    this.onInit();
  },
  destroyed() {
    window.removeEventListener("resize", this.tableHeightChange, true);
  },
  methods: {
    // 设置Table表头高度自适应
    tableHeightChange() {
      this.tableHeight = 0;
      setTimeout(() => {
        this.tableHeight = window.innerHeight - 250; // 表格自适应高度
      }, 100);
    },
    onInit() {
      this.postApiPurchasingPurchasinglistApi();
    },
    // 查看供应商采购清单数据
    onShow(row) {
      this.$refs["showSupplierPurchase"].form = row;
      this.$refs["showSupplierPurchase"].show();
    },
    // 上一步
    onPrevious() {
      this.$router.push({
        name: "采购单生成",
      });
    },
    // 查询采购清单列表-----------------------接口
    postApiPurchasingPurchasinglistApi() {
      return new Promise((resolve) => {
        let params = {
          purchasingPlanId: this.$route.query.planId,
          size: this.pages.pageSize,
          current: this.pages.page,
        };
        postApiPurchasingPurchasinglist(params).then((res) => {
          if (res.retCode == 2000) {
            this.data = res.data.records;
            console.log(res);
            this.pages.pageTotal = res.data.total;
          }
          resolve();
        });
      });
    },
    // 生成采购需求汇总表----------------------------------接口
    postApiPurchasingSupplierPurchasingPurchasingSummaryApi() {
      this.$cu.showLoading();
      return new Promise((resolve) => {
        let params = {
          purchasingPlanId: this.$route.query.planId,
        };
        postApiPurchasingSupplierPurchasingPurchasingSummary(params).then(
          (res) => {
            this.$cu.hideLoading(2);
            if (res.retCode == 2000) {
              window.location.href = res.data;
            } 
            resolve();
          }
        );
      });
    },
    // 生成采购单
    getApiPurchasingPoorderelistGeneratePoOrderApi() {
      this.$cu.showLoading();
      return new Promise((resolve) => {
        getApiPurchasingPoorderelistGeneratePoOrder().then((res) => {
          this.$cu.hideLoading(1.5);
          if (res.retCode == 2000) {
            this.postApiFoodBookServiceAuditConfirmPurchasingPlanApi();
          } 
          resolve();
        });
      });
    },
    // 下一步生成供应商采购清单-----------------------------------接口
    postApiFoodBookServiceAuditConfirmPurchasingPlanApi() {
      return new Promise((resolve) => {
        let params = {
          nextStep: 0,
          planId: this.$route.query.planId,
          type: 1,
        };
        postApiFoodBookServiceAuditConfirmPurchasingPlan(params).then((res) => {
          if (res.retCode == 2000) {
            this.$router.push({
              name: "采购单查看",
              query: {
                id: this.$route.query.planId,
              },
            });
          }
          resolve();
        });
      });
    },
  },
  watch: {},
  computed: {},
};
</script>

<style scoped>
</style>